1
//--------------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // File: ThreadPerTaskScheduler.cs
7 //--------------------------------------------------------------------------
9 using System
.Collections
.Generic
;
12 namespace System
.Threading
.Tasks
.Schedulers
14 /// <summary>Provides a task scheduler that dedicates a thread per task.</summary>
15 public class ThreadPerTaskScheduler
: TaskScheduler
17 /// <summary>Gets the tasks currently scheduled to this scheduler.</summary>
18 /// <remarks>This will always return an empty enumerable, as tasks are launched as soon as they're queued.</remarks>
19 protected override IEnumerable
<Task
> GetScheduledTasks() { return Enumerable.Empty<Task>(); }
21 /// <summary>Starts a new thread to process the provided task.</summary>
22 /// <param name="task">The task to be executed.</param>
23 protected override void QueueTask(Task task
)
25 new Thread(() => TryExecuteTask(task
)) { IsBackground = true }
.Start();
28 /// <summary>Runs the provided task on the current thread.</summary>
29 /// <param name="task">The task to be executed.</param>
30 /// <param name="taskWasPreviouslyQueued">Ignored.</param>
31 /// <returns>Whether the task could be executed on the current thread.</returns>
32 protected override bool TryExecuteTaskInline(Task task
, bool taskWasPreviouslyQueued
)
34 return TryExecuteTask(task
);